package com.service.Impl;

import com.entity.Coupon;
import com.entity.LotteryPrize;
import com.entity.Product;
import com.exception.AddException;
import com.exception.UpdateException;
import com.github.pagehelper.PageInfo;
import com.mapper.CouponMapper;
import com.mapper.LotteryPrizeMapper;
import com.mapper.ProductMapper;
import com.service.LotteryPrizeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName LotteryPrizeServiceImpl
 * @Description TODO
 * @Author Kx
 * @Date 2025/6/18 9:29
 * @Version 1.0
 */
@Service("lotteryPrizeService")
public class LotteryPrizeServiceImpl implements LotteryPrizeService {
    private final LotteryPrizeMapper lotteryPrizeMapper;
    private final ProductMapper productMapper;
    private final CouponMapper couponMapper;

    public LotteryPrizeServiceImpl(LotteryPrizeMapper lotteryPrizeMapper, ProductMapper productMapper, CouponMapper couponMapper) {
        this.lotteryPrizeMapper = lotteryPrizeMapper;
        this.productMapper = productMapper;
        this.couponMapper = couponMapper;
    }

    /**
     * @param lotteryPrize : 奖品信息
     * @author: kx
     * @date: 2025/6/18 9:35
     * @description: 添加抽奖奖品信息
     */
    @Transactional
    @Override
    public void addLotteryPrize(LotteryPrize lotteryPrize) {
        // 如果奖品类型为积分，则库存为0,关联目标ID为空
        if (lotteryPrize.getType() == 1) {
            lotteryPrize.setStock(null);
            lotteryPrize.setTargetId(null);
        } else if (lotteryPrize.getType() == 3) { // 如果奖品类型为谢谢惠顾，则库存为0,关联目标ID为空
            lotteryPrize.setStock(null);
            lotteryPrize.setTargetId(null);
        }
        // 判断关联目标ID是否存在
        if (lotteryPrize.getTargetId() != null) {
            // 判断是否是商品
            if (lotteryPrize.getType() == 0) {
                // 获取商品奖品信息
                Product product = productMapper.findByProductId(lotteryPrize.getTargetId());
                // 设置奖品名称
                lotteryPrize.setName(product.getTitle());
            } else if (lotteryPrize.getType() == 2) {
                // 获取优惠券奖品
                Coupon coupon = couponMapper.findById(lotteryPrize.getTargetId());
                // 设置奖品名称
                lotteryPrize.setName(coupon.getName());
            }
        }
        // 设置创建时间
        lotteryPrize.setCreateTime(LocalDateTime.now());
        try {
            lotteryPrizeMapper.addLotteryPrize(lotteryPrize);
        } catch (Exception e) {
            throw new AddException(1002, "添加奖品信息失败");
        }
    }

    /**
     * @author: kx
     * @date: 2025/6/18 10:24
     * @param lotteryPrize : 奖品信息
     * @description: 修改奖品信息
    */
    @Override
    public void updateLotteryPrize(LotteryPrize lotteryPrize) {
        // 设置更新时间
        lotteryPrize.setUpdateTime(LocalDateTime.now());
        try {
            lotteryPrizeMapper.updateLotteryPrize(lotteryPrize);
        } catch (Exception e) {
            throw new UpdateException(1003, "修改奖品信息失败");
        }
    }

    /**
     * @author: kx
     * @date: 2025/6/18 11:08
     * @return java.util.List<com.entity.LotteryPrize>
     * @description: 查询所有上架奖品信息
    */
    @Override
    public List<LotteryPrize> getAllLotteryPrizes() {
        if (!lotteryPrizeMapper.getAllLotteryPrizes().isEmpty()) {
            return lotteryPrizeMapper.getAllLotteryPrizes();
        }
        throw new UpdateException(1001, "没有查询到奖品信息");
    }

    /**
     * @author: kx
     * @date: 2025/6/19 11:16
     * @param pageNum : 页码
     * @param pageSize : 每页数量
     * @return java.util.List<com.entity.LotteryPrize>
     * @description: 分页查询所有上架奖品信息
    */
    @Override
    public List<LotteryPrize> getPagedLotteryPrizes(Integer pageNum, Integer pageSize) {
        if (!lotteryPrizeMapper.getPagedLotteryPrizes(pageNum, pageSize).isEmpty()) {
            return lotteryPrizeMapper.getPagedLotteryPrizes(pageNum, pageSize);
        }
        throw new UpdateException(1001, "没有查询到奖品信息");
    }

    /**
     * @author: kx
     * @date: 2025/6/19 11:37
     * @param keyword : 关键字
     * @param pageNum : 页码
     * @param pageSize : 每页数量
     * @return com.github.pagehelper.PageInfo<com.entity.LotteryPrize>
     * @description: 根据关键字分页查询奖品信息
    */
    @Override
    public PageInfo<LotteryPrize> searchLotteryPrizesByKeyWord(String keyword, Integer pageNum, Integer pageSize) {
        if (!lotteryPrizeMapper.searchLotteryPrizesByKeyWord(keyword, pageNum, pageSize).isEmpty()) {
            return new PageInfo<>(lotteryPrizeMapper.searchLotteryPrizesByKeyWord(keyword, pageNum, pageSize));
        }
        throw new UpdateException(1001, "没有查询到奖品信息");
    }
}
